<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :md="6" :sm="8">
            <a-form-item label="产品名称">
              <!--<a-input placeholder="请输入商品名称" v-model="queryParam.name"></a-input>-->
              <j-input placeholder="请输入产品名称" v-model="queryParam.name"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="货号">
              <!--<a-input placeholder="请输入货号" v-model="queryParam.productSn"></a-input>-->
              <j-input placeholder="请输入货号" v-model="queryParam.productSn"></j-input>

            </a-form-item>
          </a-col>
        <template v-if="toggleSearchStatus">
          <a-col :md="6" :sm="8">
            <a-form-item label="产品类型">
<!--              <j-dict-select-tag v-model="queryParam.productCategoryId" placeholder="请选择产品分类" dictCode="pms_product_category,name,id,show_status='1' order by sort"/>-->
              <a-select
                v-model="queryParam.productCategoryId"
                style="width: 100%"
                :getPopupContainer="node => node.parentNode"
                placeholder="请选择产品类型">
                <a-select-option v-for="(category,categoryIndex) in categoryList" :key="categoryIndex.toString()"
                                 :value="category.id">
                  {{ category.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          </template>
          <a-col :md="6" :sm="8" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button @click="recycleBinVisible=true" icon="hdd">回收站</a-button>
      <!--<a-button type="primary" icon="download" @click="handleExportXls('商品信息')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>-->
      <!--<a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:type}"
        @change="handleTableChange"
        :customRow="clickThenCheck">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleCopy(record)">拷贝</a>
          <a-divider type="vertical" />
          <a @click="handlePicDetail(record)">主图</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
<!--              <a-menu-item>
                <a href="javascript:;" @click="handleStatusDetail(record)">商品标签</a>
              </a-menu-item>-->
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="轮播主图" :key="refKeys[0]" :forceRender="true">
        <pms-product-pics-list ref="PmsProductPicsList"></pms-product-pics-list>
      </a-tab-pane>
    </a-tabs>

    <!-- 表单区域 -->
    <pmsProduct-modal ref="modalForm" @ok="modalFormOk"></pmsProduct-modal>

    <!-- 状态修改快捷方式 -->
    <pms-product-status-modal ref="productStatusModal" @ok="modalFormOk"></pms-product-status-modal>
    <!-- 主图修改快捷方式 -->
    <pms-product-pic-modal ref="productPicModal" @ok="modalFormOk"></pms-product-pic-modal>

    <pms-product-recycle-bin-modal :visible.sync="recycleBinVisible" @ok="modalFormOk"/>
  </a-card>
</template>

<script>
  import PmsProductModal from './modules/PmsProductModalV2'
  import PmsProductStatusModal from './modules/PmsProductStatusModal'
  import PmsProductPicModal from './modules/PmsProductPicModal'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { httpAction, deleteAction, postAction, getAction } from '@/api/manage'
  import PmsProductPicsList from './PmsProductPicsList'
  import JInput from '@/components/jeecg/JInput'
  import PmsProductRecycleBinModal from './modules/PmsProductRecycleBinModal'

  export default {
    name: "PmsProductList",
    mixins:[JeecgListMixin],
    components: {
      PmsProductModal,
      PmsProductStatusModal,
      PmsProductPicModal,
      PmsProductPicsList,
      JInput,
      PmsProductRecycleBinModal,
    },
    data () {
      return {
        refKeys: ['PmsProductPicsList' ],
        description: '产品管理',
        recycleBinVisible: false,
        categoryList: [],
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title: '品牌',
            align:"center",
            dataIndex: 'brandName'
          },
          {
            title: '分类',
            align:"center",
            dataIndex: 'productCategoryName'
          },
          {
            title: '名称',
            align:"center",
            dataIndex: 'name'
          },
          {
            title: '货号',
            align:"center",
            dataIndex: 'productSn'
          },
          {
            title: '建议售价',
            align:"center",
            dataIndex: 'originalPrice'
          },
          {
            title: '排序',
            align:"center",
            dataIndex: 'sort'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            width:260,
            scopedSlots: { customRender: 'action' },
          }
        ],
    type: "radio",
		url: {
          edit: "/pms/pmsProduct/edit",
          editProduct: "/pms/pmsProduct/editProductStatus",
          list: "/pms/pmsProduct/list",
          delete: "/pms/pmsProduct/delete",
          deleteBatch: "/pms/pmsProduct/deleteBatch",
          wxmaSyncBatch: "/pms/pmsProduct/wxmaSyncBatch",
          wxmaGetProduct: "/pms/pmsProduct/wxmaGetProduct",
          exportXlsUrl: "pms/pmsProduct/exportXls",
          importExcelUrl: "pms/pmsProduct/importExcel",
          getProductCategorys: '/pms/pmsProductCategory/getProductCategorys',
        },
       }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    created() {
      this.getCategoryList()
    },
    methods: {
      getCategoryList() {
        getAction(this.url.getProductCategorys, {}).then(res => {
          if (res.success) {
            this.categoryList = res.result
          } else {
            console.log(res.message)
          }
        })
      },
      clickThenCheck(record) {
        return {
          on: {
            click: () => {
              this.onSelectChange((''+record.id).split(","), [record]);
            }
          }
        };
      },
      onSelectChange(selectedRowKeys, selectionRows) {
        this.selectedRowKeys = selectedRowKeys;
        this.selectionRows = selectionRows;
        this.$refs.PmsProductPicsList.getMain(this.selectedRowKeys[0],this.selectionRows[0].productSn);
      },
      onClearSelected() {
        this.selectedRowKeys = [];
        this.selectionRows = [];
        //this.$refs.PmsProductPicsList.queryParam.mainId = null;
        this.$refs.PmsProductPicsList.selectedRowKeys = [];
        this.$refs.PmsProductPicsList.selectionRows = [];
        this.$refs.PmsProductPicsList.loadData();
      },
      handleStatusDetail (record) {
        this.$refs.productStatusModal.show(record);
      },
      handlePicDetail (record) {
        this.$refs.productPicModal.show(record);
      },
      handleDelete: function (id) {
        let that = this;
        deleteAction(that.url.delete, {id: id}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
            this.$refs.PmsSkuStockList.loadData();
            this.$refs.PmsProductPicsList.loadData();
            this.$refs.PmsProductQualificationPicsList.loadData();
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      searchQuery:function(){
        this.selectedRowKeys = [];
        this.selectionRows = [];
        //this.$refs.PmsProductPicsList.queryParam.mainId = null;
        this.$refs.PmsProductPicsList.loadData();
        this.$refs.PmsProductPicsList.selectedRowKeys = [];
        this.$refs.PmsProductPicsList.selectionRows = [];
        this.loadData(1);
      },

    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>